

#include "main.h"

/**
 * @brief MAC 地址
 *
 */
const char *if_addr[] = {
	"eth0",
	"eth0.1",
	"eth0.2",
	"eth1"};

/**
 * @brief Get the mac object
 *
 * @param put_buffer
 * @return uint8_t
 */
uint8_t get_mac(char *put_buffer)
{
	char buf[1024] = {0};
	char mac_string[1024] = {0};
	uint8_t i;

	if (put_buffer == NULL)
	{
		printf("get_mac: (put_buffer == NULL)\n");
		return FALSE;
	}

	/**
	 * @brief get the mac file
	 *
	 */
	for (i = 0; i < MAC_NUM; i++)
	{
		memset(mac_string, 0x00, sizeof(mac_string));
		sprintf(mac_string, "/sys/class/net/%s/address", if_addr[i]);

		FILE *fp = fopen(mac_string, "r");

		if (!fp)
		{
			continue;
		}

		/**
		 * @brief get the message
		 */
		memset(buf, 0x00, sizeof(buf));
		if (fgets(buf, sizeof(buf) - 1, fp) != 0)
		{
			sprintf(put_buffer + strlen(put_buffer), "%s: %s", if_addr[i], buf);
			fclose(fp);
		}

	}

	return TRUE;
}

/**
 * @brief Get the flashSize object
 *
 * @return uint64_t
 */
long get_flashSize(void)
{

	char major[64] = {0};
	char minor[64] = {0};
	char name[64] = {0};

	char buf[1024] = {0};
	long size = 0;

	long size_toral = 0;

	FILE *fp = fopen("/proc/partitions", "r");

	if (fp != NULL)
	{
		// printf("fopen /proc/partitions\n");
		while (fgets(buf, sizeof(buf) - 1, fp) != 0)
		{
			sscanf(buf, "%s %s %d %s",
				   major, minor, &size, name);
			
			// printf("major=%s, minor=%s, size=%d, name=%s\n", major, minor, size, name);
			
			if (!strcmp(name, "mmcblk0"))
				size_toral += size;

			memset(major, 0x00, sizeof(major));
			memset(minor, 0x00, sizeof(minor));
			memset(name, 0x00, sizeof(name));
			memset(buf, 0x00, sizeof(buf));
		}

		size = size_toral / 1024 ;
	}
	else
	{
		printf("ERROR: failed to open /proc/partitions info file.\n");
		return -1;
	}

	fclose(fp);
	return size;
}

/**
 * @brief Get the memory object
 *
 * @return int32_t
 */
int32_t get_memory(void)
{
	char buf[1024] = {0};
	char temp_name[64] = {0};
	char temp_memSize[64] = {0};
	char temp_unit[64] = {0};

	int32_t mem_size = 0;

	FILE *fp = fopen("/proc/meminfo", "r");
	if (fp != NULL)
	{
		fgets(buf, sizeof(buf), fp);

		sscanf(buf, "%s %s %s",
			   temp_name, temp_memSize, temp_unit);

		if (strcmp(temp_name, "MemTotal:") == 0)
		{
			mem_size = atoi(temp_memSize) / 1000;
			//针对其他内存使用可能让系统获得的实际使用量减少，需要修正
			mem_size = (mem_size / 256 + 1 ) * 256;
		}
	}
	else
	{
		printf("ERROR: failed to open /proc/meminfo info file.\n");
		return -1;
	}

	fclose(fp);

	return mem_size;
}



/**
 * @brief Get the cpuFreq object
 *
 * @return int32_t
 */
int32_t get_cpuFreq(void)
{
	int32_t freq = 0;
	char buf[512] = {0};
	const char *filepath = "/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq";

    // Check if the file exists and has read permission
    if (access(filepath, R_OK) == -1) {
        //perror("Error accessing cpu freq");
        return 800;
    }

	FILE *fp = fopen(filepath, "r");

	if (fp != NULL)
	{
		fgets(buf, sizeof(buf), fp);
		freq = atoi(buf) / 1000;
	}
	else
	{
		printf("ERROR: failed to open.\n");
		fclose(fp);
		return -1;
	}

	fclose(fp);
	return freq;
}
